package com.example.imtoken.modules.main.wallet.manage.wallet.imp.mnemonic

import android.content.Intent
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.InjectorUtils
import com.example.imtoken.databinding.ActivityMnemonicImportWalletBinding
import com.example.imtoken.modules.main.MainActivity
import com.example.imtoken.modules.scanning.ScanningActivity

class MnemonicImportWalletActivity :
    BaseActivity<MnemonicImportWalletViewModel, ActivityMnemonicImportWalletBinding>(
        MnemonicImportWalletViewModel::class.java
    ) {

    private val coin by lazy {
        intent.getStringExtra("coin")
    }

    private val viewModelF by lazy {
        getViewModelFactory(InjectorUtils.getMnemonicImportWalletViewModel())
    }

    private var isPwShow = false

    override fun initLayoutId(): Int {
        return R.layout.activity_mnemonic_import_wallet
    }

    override fun init() {
        initBar(true, R.color.fragment_bg)
        dataBinding.viewModel = viewModelF
        initTitle()
        initClick()
        initObserve()
    }

    private fun initObserve() {
        viewModelF.mnemonic.observe(this) {
            viewModelF._mnemonic = it
        }
        viewModelF.pass1.observe(this) {
            viewModelF._pass1 = it
        }
        viewModelF.pass2.observe(this) {
            viewModelF._pass2 = it
        }
        viewModelF.isSubmit.observe(this) {
            if (it) {
                dataBinding.tvConfirm.setBackgroundResource(R.drawable.shape_create_btn_bg_true)
            } else {
                dataBinding.tvConfirm.setBackgroundResource(R.drawable.shape_create_btn_bg_false)
            }
            dataBinding.tvConfirm.isClickable = it
        }
        viewModelF.isSuccess.observe(this) {
            if (it) {
                ActivityUtils.startActivity(MainActivity::class.java)
            }
        }
    }

    private fun initClick() {
        dataBinding.include11.ivHeadBack.setOnClickListener { this.finish() }
        ClickUtils.applyGlobalDebouncing(dataBinding.include11.ivHeadFunction) {
            //扫描
            val intent = Intent(this, ScanningActivity::class.java)
            intent.putExtra("operate", "mnemonic")
            startActivityForResult(intent, 0)
        }
        dataBinding.ivPassShow.setOnClickListener {
            if (isPwShow) {
                dataBinding.etPassWord1.transformationMethod =
                    HideReturnsTransformationMethod.getInstance()
                dataBinding.etPassWord2.transformationMethod =
                    HideReturnsTransformationMethod.getInstance()
                dataBinding.ivPassShow.setImageResource(R.mipmap.ic_password_show)
            } else {
                dataBinding.etPassWord1.transformationMethod =
                    PasswordTransformationMethod.getInstance()
                dataBinding.etPassWord2.transformationMethod =
                    PasswordTransformationMethod.getInstance()
                dataBinding.ivPassShow.setImageResource(R.mipmap.ic_password_no_show)
            }
            dataBinding.etPassWord1.clearFocus()
            dataBinding.etPassWord2.clearFocus()
            isPwShow = !isPwShow
        }
        dataBinding.tvConfirm.setOnClickListener {
            if (viewModelF.verifyParams()) {
                viewModelF.impWallet2(coin)
            }
        }
    }

    private fun initTitle() {
        dataBinding.include11.clHeadHome.setBackgroundResource(R.color.fragment_bg)
        coin?.let {
            dataBinding.include11.tvHeadTitles.text =
                getString(R.string.impWalletCoin, it.toUpperCase())
        }
        dataBinding.include11.ivHeadFunction.visibility = View.VISIBLE
        dataBinding.include11.ivHeadFunction.setImageResource(R.mipmap.src_lib_eui_icon_scan)
        dataBinding.include11.vLine.visibility = View.INVISIBLE
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            val mnemonic = data?.getStringExtra("mnemonic")
            dataBinding.etMnemonic.setText(mnemonic)
        }
    }

}